////////////////////////////////////////////////////////////////////////////////
//
//	Original file Name: GameRenderer.java
//	Originating Author: Jon Whitefield
//	
//	Description: Renders the game panel and ultimately is in charge of the 
//				 Amount of level renderer there are  
//
////////////////////////////////////////////////////////////////////////////////

package com.jjw.game.screen;

import java.util.List;

import com.jjw.game.Game;
import com.jjw.game.ITickable;
import com.jjw.game.input.InputDevice;
import com.jjw.game.input.InputManager;

/**
 * Renders the game panel and ultimately is in charge of the amount of level renderer there are
 * 
 * @author Jon Whitefield
 *
 */
public class GameRenderer implements ITickable {
	
	//--------------------------------------------------------------------------
	//
	//Properties
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	// Public
	//----------------------------------
	
	//----------------------------------
	//  Getters / Setters
	//----------------------------------
	
	//----------------------------------
	// Private
	//----------------------------------
	
	/**
	 * The screen controller we are using
	 */
	private ScreenController screenController; 
	
	/**
	 * List of the level renderer we are rendering
	 */
	private LevelRenderer[] levelRenderers = null;
	
	//--------------------------------------------------------------------------
	//
	//  Methods
	//
	//--------------------------------------------------------------------------
	
	//----------------------------------
	// Public
	//----------------------------------
	
	public GameRenderer( ScreenController screenController ) {

		//Set the screen controller 
		this.screenController = screenController;
		
		//Create the level renderer
		this.createLevelRenderers();
	}

	/**
	 * Render the game renderer, which in turn renderer the level renderer
	 */
	public void render() {
	
		//If there are no level renderer then we can't render them  
		if( this.levelRenderers == null )
			return;
		
		//Loop through the level renderer
		for( int i = 0; i < this.levelRenderers.length; i++ ) {
			
			//Check we have a renderer at this index
			if( this.levelRenderers[i] == null )
				continue;
			
			//Render the level
			levelRenderers[i].render();
		}		
	}
	
	//----------------------------------
	// Private
	//----------------------------------
	
	/**
	 * Create the renderer for the level 
	 */
	private void createLevelRenderers() {
		
		//Level renderer place holder 
		LevelRenderer levelRenderer = null;
				
		List<InputDevice> inputDevices = InputManager.getInstance().getAllControllers();

		//Create a new renders list 
		this.levelRenderers = new LevelRenderer[inputDevices.size()];
		
		//Loop through creating renderer for each position  
		for( int i = 0; i < inputDevices.size(); i++ ) {
			
			//Get the position of the screen which will be used in the level renderer
			int[] levelPosition = this.levelRenderDimentions(i, inputDevices.size());
			
			//Create a new screen 
			Screen s = new Screen(levelPosition[0], levelPosition[1], levelPosition[2], levelPosition[3]);
			
			//Register the screen with the screen controller
			this.screenController.registerScreen(s);
			
			//Create a new level renderer
			levelRenderer = new LevelRenderer(s, "level2", inputDevices.get(i) );
			
			//Add the renderer to the list of level renderer
			this.levelRenderers[i] = levelRenderer;
		}
	}
	
	/**
	 * Works out the dimensions for the level renderer creation
	 * @param index
	 * @param outOf
	 * @return
	 */
	private int[] levelRenderDimentions( int index, int outOf ) {
		
		//Dimensions for the level renderer
		int[] dimensions = {0,0,0,0};
				
		//Get the width and height of the game
		int width = Game.gameWidth();
		int height = Game.gameHeight();
		
		//If its either the 2nd or 4th screen then we need to offset it by half the width
		if( (index % 2) != 0 )
			dimensions[0] = width / 2;
				
		//If one then full screen
		if( outOf == 1 ){
			dimensions[2] = width;
			dimensions[3] = height;
		}
		//If 2 then half the width
		else if( outOf == 2 ){
			dimensions[2] = width/2;
			dimensions[3] = height;
			
		}
		//If 3 or 4
		else if( outOf == 3 || outOf == 4 ){
			
			//If the index is 3 or 4 set the y value to half the height
			if( index==2 || index == 3 )
				dimensions[1] = height/2;
			
			//We want half the width and height for the screen
			dimensions[2] = width/2;
			dimensions[3] = height/2;
			
			//If its the 3rd item out off 3 they will get the full width of the screen
			if( outOf == 3 && index == 2)
				dimensions[2] = width;
		}
		
		//Return the dimensions
		return dimensions;
	}
	
	//----------------------------------
	// Overridden / Implemented
	//----------------------------------
	
	@Override
	public void tick() {
		
		if(levelRenderers == null)
			return;
		
		for(int i = 0; i < levelRenderers.length; i++)
			levelRenderers[i].tick();
		
	}
}

